//
//  OJFilterListController.m
//  openJira
//
//  Created by Vincent Demay on 29/04/10.
//  Copyright 2010 Goojet. All rights reserved.
//

#import "OJFilterListController.h"
#import "OJIssuesListController.h"
#import "OJCreateIssueViewController.h"


@implementation OJFilterListController


- (id)initWithStyle:(UITableViewStyle)style {
    if (self = [super initWithStyle:style]) {
		UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"minilogo.png"]];
		self.navigationItem.titleView = imageView;
		[imageView release];
		
		UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
		temporaryBarButtonItem.title = @"Filters";
		self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
		self.navigationController.navigationBar.contentMode = UIViewContentModeScaleAspectFit;
		[temporaryBarButtonItem release];
		
		//self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"create" style:UIBarButtonItemStyleBordered target:self action:@selector(onCreateTouch:)];
		
		self.tabBarItem.title = @"Filters";
		self.tabBarItem.image = [UIImage imageNamed:@"filter.png"];
		
		self.title = @"Filters";
		
		self.hidesBottomBarWhenPushed = NO;
	
	}
    return self;
}

- (void) onCreateTouch:(id) sender {
	OJCreateIssueViewController *ctrl = [[OJCreateIssueViewController alloc] initWithStyle:UITableViewStyleGrouped];
	[self.navigationController pushViewController:ctrl animated:YES];
	[ctrl release];
}



- (void)viewDidLoad {
    [super viewDidLoad];
	
	activity = [[TTActivityLabel alloc] initWithStyle:TTActivityLabelStyleBlackBezel];
	activity.text = @"Loading...";
	//[activity sizeToFit];
	activity.frame = CGRectMake(0, 0, self.view.width, self.view.height);
	[self.view addSubview:activity];
	activity.isAnimating = YES;
	
	
	model = [[OJFilterList alloc] init];
	model.delegate = self;
	[model getFavouriteFilters];
}

- (void) object:(OJFilterList*) filter isReady:(BOOL) onError {
	activity.isAnimating = NO;
	activity.hidden = YES;
	
	if (onError) {
		// TODO
		// failed -> do something
	} else {
		[self.tableView reloadData];
	}
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [model.filters count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
	OJFilter * filter = (OJFilter*)[model.filters objectAtIndex:indexPath.row];
	
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    
    cell.textLabel.text = filter.name;
	cell.detailTextLabel.text = filter.description;
	
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	OJFilter * filter = (OJFilter*)[model.filters objectAtIndex:indexPath.row];
	OJIssuesListController *issuesController = [[OJIssuesListController alloc] initWithStyle:UITableViewStylePlain];
	issuesController.filter = filter;
	[self.navigationController pushViewController:issuesController animated:YES];
}


- (void)dealloc {
	[activity release];
	[model release];
    [super dealloc];
}


@end

